package com.xingongchang.baolebei.activity;

import org.json.JSONException;
import org.json.JSONObject;

import com.external.androidquery.callback.AjaxStatus;
import com.insthub.BeeFramework.activity.BaseActivity;
import com.insthub.BeeFramework.model.BusinessResponse;
import com.insthub.BeeFramework.view.RoundImageView;
import com.xingongchang.baolebei.R;
import com.xingongchang.baolebei.model.LoginModel;
import com.xingongchang.baolebei.model.ProtocolConst;
import com.xingongchang.baolebei.model.UserInfoModel;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

public class CompileActivity extends  BaseActivity implements BusinessResponse {
	private ImageView complie_camera,top_back;//选图像
	private EditText complie_name;//更改用户名
	private EditText complie_nickniam;//更改昵称
	private CheckBox complie_girl,complie_boy;//更改男女
	private EditText complie_youxiang;//更改邮箱
	private LinearLayout top_right_button;
	private RoundImageView userIcon;
	LoginModel loginModel;
	String pic = "";
	int sex = 1,type;
	String open_id,userName,userNick,UserEmail;
	private SharedPreferences shared;
	private SharedPreferences.Editor editor;
	ProgressDialog pd;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
    	setContentView(R.layout.compile);
    	Intent intent=getIntent();
    	type=intent.getIntExtra("user_type",0);
    	open_id=intent.getStringExtra("open_Id");
    	loginModel=new LoginModel(this);
    	loginModel.addResponseListener(this);
    	findViewById();
    	setListener();
    	init();
    }
	private void findViewById() {
		top_back=(ImageView) findViewById(R.id.top_back);
		complie_camera=(ImageView) findViewById(R.id.complie_camera);
		complie_name=(EditText) findViewById(R.id.complie_name);
		complie_nickniam=(EditText) findViewById(R.id.complie_nickniam);
		complie_girl=(CheckBox) findViewById(R.id.complie_girl);
		complie_boy=(CheckBox) findViewById(R.id.complie_boy);
		complie_youxiang=(EditText) findViewById(R.id.complie_youxiang);
		top_right_button=(LinearLayout) findViewById(R.id.top_right_button);
		userIcon=(RoundImageView) findViewById(R.id.userIcon);
	}
	private void setListener() {
		complie_boy.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				complie_boy.setChecked(true);
				complie_girl.setChecked(false);
				sex = 1;
			}
		});

		complie_girl.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				complie_boy.setChecked(false);
				complie_girl.setChecked(true);
				sex = 0;
			}
		});

		top_back.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				hideSystemKeyBoard(CompileActivity.this,complie_name );
				finish();
			}
		});
        //完成
		top_right_button.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				userName=complie_name.getText().toString().trim();
				userNick=complie_nickniam.getText().toString().trim();
				UserEmail=complie_youxiang.getText().toString().trim();
				if (sex == 1) {
					complie_boy.setChecked(true);
					complie_girl.setChecked(false);

				} else if (sex == 0) {
					complie_boy.setChecked(false);
					complie_girl.setChecked(true);
				}
				pd = new ProgressDialog(CompileActivity.this);
				pd.setMessage(CompileActivity.this.getResources().getString(R.string.hold_on));
				if(type==1){
					loginModel.setLogininfo(userName, pic, sex, userNick, "editUserInfo",1, open_id, UserEmail);
				}else if(type==2){
					loginModel.setLogininfo(userName, pic, sex, userNick, "editUserInfo",2, open_id, UserEmail);
				}
//				Toast.makeText(getApplicationContext(), ""+open_id, 0).show();
			}
		});
           
		complie_camera.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				dialog();
			}
		});

	}

	private void init() {

		top_back.setVisibility(View.VISIBLE);
		top_right_button.setVisibility(View.VISIBLE);
		complie_boy.setChecked(true);
		complie_girl.setChecked(false);
//		mUserInfoModel = new UserInfoModel(this);
//		mUserInfoModel.addResponseListener(this);
//		mUserInfoModel.getTopicUserInfo();
//		user_name.setText(name);
	}


	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode == PHOTO_REQUEST_GALLERY) {
			// 从相册返回的数据
			if (data != null) {
				// 得到图片的全路径
				Uri uri = data.getData();
				crop(uri);
			}

		} else if (requestCode == PHOTO_REQUEST_CAREMA) {
			// 从相机返回的数据
			if (hasSdcard()) {
				crop(Uri.fromFile(tempFile));
			} else {
				Toast.makeText(CompileActivity.this, "未找到存储卡，无法存储照片！",
						Toast.LENGTH_SHORT).show();
			}

		} else if (requestCode == PHOTO_REQUEST_CUT) {
			// 从剪切图片返回的数据
			if (data != null) {
				Bitmap bitmap = data.getParcelableExtra("data");
				userIcon.setImageBitmap(bitmap);
				pic = imgToBase64("", bitmap, "");
			}
			try {
				// 将临时文件删除
			} catch (Exception e) {
				e.printStackTrace();
			}

		}
	}
	@Override
	public void OnMessageResponse(String url, JSONObject jo, AjaxStatus status)
			throws JSONException {
		if(url.endsWith(ProtocolConst.DISANFANG)){
//			setListener();
			Intent intent=new Intent(CompileActivity.this,BaolebeiActivity.class);
			startActivity(intent);
			overridePendingTransition(R.anim.push_up_in, R.anim.push_up_out);
//			if(type==1){
//				editor.putString("uid", open_id);
//				editor.commit();
//				loginModel.snsLogin(open_id, "login",1);
//			}else if(type==2){
//				editor.putString("uid", open_id);
//				editor.commit();
//				loginModel.snsLogin(open_id, "login",2);
//			}
		  pd.dismiss();
		  finish();
			
		}
	}
}
